﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Diagnostics;
using GestureDemo.Transition;

namespace GestureDemo
{
    public partial class MainPage : PhoneApplicationPage
    {
        private bool _isHorizontal = false;
        private PageTransition pageTransition;
        private int pageHostBaseIndex;
        private Rectangle[] pageHosts;
        private bool gestureInProgress;
        private bool animationInProgress;
        private bool gestureHadFlick;
        private bool canGoNextPage;
        private bool canGoPrevPage;
        private double dragFactor;

        public MainPage()
        {
            this.pageHosts = new Rectangle[3];
            InitializeComponent();
            pageTransition = new SildeTransition();
            pageHosts[0] = Left;
            pageHosts[1] = Center;
            pageHosts[2] = Right;
            pageTransition.Attach(this.LayoutRoot, Left, Center, Right);
            Rectangle baseRect = this.pageHosts[this.pageHostBaseIndex];
            baseRect.SizeChanged += new SizeChangedEventHandler(baseRect_SizeChanged);
        }

        void baseRect_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Debug.WriteLine("sizechanged-->");
        }
        //1.手势监控开始
        private void GestureListener_GestureBegin(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            this.gestureInProgress = !this.animationInProgress;
            this.gestureHadFlick = false;
            //根据是否能有数据来判断是否可以加载新的数据
            //this.canGoNextPage = Enumerable.Any<UIElement>(this.pageHosts[(this.pageHostBaseIndex + 1) % 3].cArticles.get_Children());
            //this.canGoPrevPage = Enumerable.Any<UIElement>(this.pageHosts[(this.pageHostBaseIndex + 2) % 3].cArticles.get_Children());
        }
        //2.拖拽开始
        private void GestureListener_DragStarted(object sender, DragStartedGestureEventArgs e)
        {
            this.dragFactor = 0.0;
        }
        //3.拖拽过程中
        private void DragDelta(object sender, DragDeltaGestureEventArgs e)
        {
            this.gestureInProgress = true;
            double value = this.dragFactor - e.HorizontalChange;//拖拽的偏移量
        }
        //4.轻轻抚去
        private void GestureListener_Flick(object sender, FlickGestureEventArgs e)
        {
            this.gestureHadFlick = true;
            Debug.WriteLine("flick");
        }

        //5.拖拽结束
        private void GestureListener_DragCompleted(object sender, DragCompletedGestureEventArgs e)
        {
            this.gestureInProgress = false;
            if (e.HorizontalChange < 0)
            {
                Storyboard storyboard = new Storyboard();
                DoubleAnimation doubleAnimation = new DoubleAnimation();
                doubleAnimation.To = (double)(this.pageHostBaseIndex + 1);
                doubleAnimation.Duration = this.GetDuration((double)(this.pageHostBaseIndex + 1));
                Storyboard.SetTarget(doubleAnimation, this.pageTransition);
                Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(PageTransition.FractionalBaseIndexProperty));
                storyboard.Children.Add(doubleAnimation);
                storyboard.Completed += storyboard_Completed;
                this.animationInProgress = true;
                base.IsHitTestVisible = false;
                storyboard.Begin();
            }
            else
            {
                Storyboard storyboard = new Storyboard();
                DoubleAnimation doubleAnimation = new DoubleAnimation();
                doubleAnimation.To = (double)(this.pageHostBaseIndex - 1);
                doubleAnimation.Duration = this.GetDuration((double)(this.pageHostBaseIndex - 1));
                Storyboard.SetTarget(doubleAnimation, this.pageTransition);
                Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(PageTransition.FractionalBaseIndexProperty));
                storyboard.Children.Add(doubleAnimation);
                storyboard.Completed += storyboard_Completed1;
                this.animationInProgress = true;
                base.IsHitTestVisible = false;
                storyboard.Begin();
            }
            
        }

        void storyboard_Completed(object sender, EventArgs e)
        {
            //TODO 更新标记
            this.pageHostBaseIndex = (this.pageHostBaseIndex + 1) % 3;
            this.pageTransition.FractionalBaseIndex = this.pageHostBaseIndex;
            this.animationInProgress = false;
            base.IsHitTestVisible = true;
        }

        void storyboard_Completed1(object sender, EventArgs e)
        {
            //TODO 更新标记
            this.pageHostBaseIndex = (this.pageHostBaseIndex + 2) % 3;
            this.pageTransition.FractionalBaseIndex = this.pageHostBaseIndex;
            this.animationInProgress = false;
            base.IsHitTestVisible = true;
        }

        private Duration GetDuration(double index)
        {
            double num = Math.Abs(index - this.pageTransition.FractionalBaseIndex);
            double num2 = num * this.pageTransition.AnimationDuration;
            TimeSpan timeSpan = TimeSpan.FromMilliseconds(num2);
            return new Duration(timeSpan);
        }
        //6.操作监听结束
        private void GestureListener_GestureCompleted(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            this.gestureInProgress = false;
        }
        
        //7.Tap
        private void GestureListener_Tap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            Debug.WriteLine("tap");
        }
    }
}